<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Euclidean distance between polyhedra</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/eucl_dist_poly.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Euclidean distance between polyhedra</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.2.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/09/05</span>
<span class="comment">%</span>
<span class="comment">% Given two polyhedra C = {x | A1*x &lt;= b1} and D = {x | A2*x &lt;= b2}, the</span>
<span class="comment">% distance between them is the optimal value of the problem:</span>
<span class="comment">%           minimize    || x - y ||_2</span>
<span class="comment">%               s.t.    A1*x &lt;= b1</span>
<span class="comment">%                       A2*y &lt;= b2</span>

<span class="comment">% Input data</span>
randn(<span class="string">'state'</span>,0);
rand(<span class="string">'state'</span>,0);

n  = 5;
m1 = 2*n;
m2 = 3*n;
A1 = randn(m1,n);
A2 = randn(m2,n);
b1 = rand(m1,1);
b2 = rand(m2,1) + A2*randn(n,1);

<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">x(n)</span> <span class="string">y(n)</span>
    minimize (norm(x - y))
    A1*x &lt;= b1;
    A2*y &lt;= b2;
cvx_end

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------'</span>);
disp(<span class="string">'The distance between the 2 polyhedra C and D is: '</span> );
disp([<span class="string">'dist(C,D) = '</span> num2str(cvx_optval)]);
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling sedumi: 31 variables, 11 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 11, order n = 28, dim = 32, blocks = 2
nnz(A) = 136 + 0, nnz(ADA) = 121, nnz(L) = 66
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.15E+01 0.000
  1 :   3.14E-02 5.51E+00 0.000 0.4801 0.9000 0.9000   3.34  1  1  3.4E+00
  2 :  -5.66E-01 1.54E+00 0.000 0.2799 0.9000 0.9000   1.86  1  1  1.2E+00
  3 :  -4.73E-01 4.06E-01 0.000 0.2631 0.9000 0.9000   1.31  1  1  2.9E-01
  4 :  -5.06E-01 3.42E-02 0.000 0.0843 0.9900 0.9900   1.09  1  1  2.3E-02
  5 :  -5.07E-01 1.24E-02 0.000 0.3638 0.9000 0.9000   1.03  1  1  8.3E-03
  6 :  -5.08E-01 3.13E-04 0.000 0.0252 0.9900 0.8509   1.01  1  1  2.9E-04
  7 :  -5.09E-01 2.63E-06 0.286 0.0084 0.9990 0.9931   1.00  1  1  2.4E-06
  8 :  -5.09E-01 6.39E-09 0.248 0.0024 0.9990 0.9990   1.00  1  1  9.2E-09

iter seconds digits       c*x               b*y
  8      0.0   Inf -5.0856705170e-01 -5.0856704680e-01
|Ax-b| =   2.1e-09, [Ay-c]_+ =   9.2E-09, |x|=  1.8e+00, |y|=  1.7e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    4.000E-02    1.000E-02    
Max-norms: ||b||=1, ||c|| = 2.829728e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 48.6227.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.508567
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 0.50857
</pre>
</div>
</body>
</html>